package Database;

import Hashing.ShazamHash;
import Hashing.SongsFinger;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

public class Insert {
    public static void SongInsert(Connection conn,File sample) throws  Exception{
        String sql = "insert into song(name)"+
                    "values(?)";
        conn.setAutoCommit(false);
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,sample.getName());
        psmt.executeUpdate();
        conn.commit();
        psmt.close();
    }

    public static void FingerInsert(Connection conn, File sample,int id) throws Exception{
        ArrayList<ShazamHash> sampleHashList = SongsFinger.makeFinger(sample ,id);

        String sql = "insert into finger(id,finger_id,offset)"+
                "values(?,?,?)";
        PreparedStatement psmt = conn.prepareStatement(sql);
        conn.setAutoCommit(false);
        for(int i=0;i<sampleHashList.size();i++){
            psmt.setInt(1,id);
            psmt.setInt(2,sampleHashList.get(i).finger_id);
            psmt.setInt(3,sampleHashList.get(i).offset);
            psmt.addBatch();
        }
        psmt.executeBatch();
        conn.commit();
        psmt.clearBatch();

        psmt.close();
    }

}
